﻿
Imports System
Imports System.Data
Imports System.String
Imports System.Text
Imports System.Collections

''' <summary>
''' 銷售查詢
''' </summary>
''' <remarks></remarks>
Public Class Frm_OFSTBackSelect
    Implements IDServer.IDServer.IDServer
    Private MasterDS As DataSet
    Private DetailDS As DataSet
    Private WithEvents MainForm As Frm_MainForm


#Region "Property"

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#End Region
#Region "接口"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function
#End Region
    ''' <summary>
    ''' 填充狀態
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub OrdeStatus()
        Try
            Dim Dset As New DataSet
            Dim sql As String = ""
            sql &= "select ListA from BSC_OtherDefineB  where ItemNo = '16'"
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CBOrdeStatus.DataSource = Dset.Tables(0)
            Me.CBOrdeStatus.DisplayMember = "ListA"


        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Sub
    ''' <summary>
    ''' 填充操作員
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Type()
        Try
            Dim Dset As New DataSet
            Dim sql As String = ""
            sql &= "select distinct fOperaterMan from sal_saleordeA "
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CBfOperaterMan.DataSource = Dset.Tables(0)
            Me.CBfOperaterMan.DisplayMember = "fOperaterMan"


        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Sub


    Private Sub BindMasterGrid()
        Dim mTable As New DataTable()
        Try
            With mTable
                .Columns.Add("CurrentBill")
                .Columns.Add("NextNo")
                .Columns.Add("fOrdeStatus")
                .Columns.Add("FTAmount")
                .Columns.Add("fDSIA")
                .Columns.Add("fBSI")
                .Columns.Add("fOperaterMan")
                .Columns.Add("fOperateDate")

            End With
            MasterDS = New DataSet()
            MasterDS.Tables.Add(mTable)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)

            With Me.DataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(0).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With
        Catch ex As Exception
            MsgBox("BindMasterGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try

    End Sub
    Private Function BindHashTable(Optional ByVal Index As Integer = 0) As Hashtable
        Dim mHash As New Hashtable
        Try
            With mHash
                Select Case Index

                    Case 0

                        .Add("CurrentBill", "銷售單號")
                        .Add("NextNo", "T  單")
                        .Add("fOrdeStatus", "狀態")
                        .Add("FTAmount", "總金額")
                        .Add("fDSIA", "實收訂金")
                        .Add("fBSI", "尾數")
                        .Add("fOperaterMan", "操作員")
                        .Add("fOperateDate", "操作日期")

                End Select
            End With
            Return mHash
        Catch ex As Exception
            Return Nothing
            MsgBox("BindHashTable" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mHash = Nothing

        End Try
    End Function


    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub Frm_OFSTBackSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.OrdeStatus()
        Me.Type()
        BindMasterGrid()


    End Sub

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        Dim Sql As String = ""
        Dim WhereStr As String = ""
        Try
            WhereStr += " Where 1=1 "
            ''銷售單號
            If Me.TBSaleOrdeNo.Text.Trim <> "" Then
                WhereStr += " and m.fSaleOrdeNo ='" & Me.TBSaleOrdeNo.Text.Trim & "'"
            End If

            ''退換單
            If Me.TBUpNo.Text.Trim <> "" Then
                WhereStr += "  and  n.fsaleordeno  ='" & Me.TBUpNo.Text.Trim & " '"
            End If

            If Me.CBOrdeStatus.Text.Trim <> "" Then
                WhereStr += "  and  c.listA = '" & Me.CBOrdeStatus.Text.Trim & "'"
            End If

            If Me.CBfOperaterMan.Text.Trim <> "" Then
                WhereStr += "  and  m.fOperaterMan = '" & Me.CBfOperaterMan.Text.Trim & "'"
            End If

            If IsDate(Me.DateTimePicker1.Value) = True Then
                WhereStr += "  and convert(nvarchar(10),m.fOperateDate,21)>='" & Format(Me.DateTimePicker1.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "'"
            End If

            If IsDate(Me.DateTimePicker2.Value) = True Then
                WhereStr += "  and convert(nvarchar(10),m.fOperateDate,21)<='" & Format(Me.DateTimePicker2.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "'"
            End If

            Sql = "select distinct m.fsaleordeno as CurrentBill ,n.fsaleordeno as NextNo ,c.listA as fOrdeStatus ," & vbCrLf
            Sql &= "isnull(m.FTAmount, '0.00') as FTAmount,isnull(R.fDSIA, '0.00') as fDSIA,isnull(R.fBSI, '0.00') as fBSI," & vbCrLf
            Sql &= "M.fOperaterMan,M.fOperateDate " & vbCrLf
            Sql &= " From sal_saleordea as m left join sal_saleordea n   " & vbCrLf
            Sql &= " on n.fupno=m.fsaleordeno left join BSC_OtherDefineB c on m.fOrdeStatus=c.listcode and c.ItemNo='16' " & vbCrLf
            Sql &= " left join (select * from Sal_InvoiceA where FType='DEPOSIT') R ON M.fSaleOrdeNo =R.FSaleOrdeNo  " & vbCrLf
            Sql &= " left join SAL_SALEORDEb  d on m.fsaleordeno=d.fsaleordeno  " & vbCrLf
            Sql &= WhereStr
            MasterDS = New DataSet
            MasterDS = ReturnDataset(Sql)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)

        Catch ex As Exception
            MessageBox.Show("ReturnWhereSql:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint

    End Sub

    Private Sub DataGridView1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
        FormatDgrd(DataGridView1, "fOrdeStatus")
    End Sub
End Class
